package edu.isquen.webservice.weather;

import java.net.URL;

public class GLobalWeatherUtil {
	public static GlobalWeatherLocator weatherSLocator = null;
	static String url = "http://www.webservicex.net/globalweather.asmx?wsdl";

	public static GlobalWeatherLocator getWeatherWebServiceLocator() {
		if (weatherSLocator == null) {
			weatherSLocator = new GlobalWeatherLocator();
		}
		return weatherSLocator;
	}

	public static GlobalWeatherSoap getWeatherWebService() {
		GlobalWeatherSoap weatherWebService = null;
		try {
			weatherWebService = (GlobalWeatherSoap) getWeatherWebServiceLocator()
					.getGlobalWeatherSoap(new URL(url));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return weatherWebService;
	}
	
	public static String getDefaultCityWeatherInfo(){
		return getWeatherInfoByCity("","");
	}
	
	public static String getWeatherInfoByCity(String cityName, String countryName){
		String weatherInfo;
		try {
			if(cityName==null || cityName.equals("")){
				cityName="guangzhou";
			}
			if(countryName==null || countryName.equals("")){
				countryName="china";
			}
			weatherInfo = getWeatherWebService().getWeather(cityName, countryName);
		} catch (Exception e) {
			e.printStackTrace();
			weatherInfo="";
		}
		return weatherInfo;
	}

	public static void main(String[] agrs) {
		try {
			String cityName="guangzhou";
			String countryName="china";
			String weatherMsg = getWeatherWebService().getWeather(cityName, countryName);
			System.out.println(weatherMsg);
		} catch (Exception e) {
		}
	}
}
